package com.bq.gametrade.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bq.gametrade.HttpLink;
import com.bq.gametrade.R;
import com.bq.gametrade.adapter.RedEnvelopeListAdapter;
import com.bq.gametrade.base.BaseActivity;
import com.bq.gametrade.bean.RedEnvelopeList;
import com.bq.gametrade.bean.RedEnvelopeListDetails;
import com.bq.gametrade.bean.UserInfoDetails;
import com.bq.gametrade.http.VRHttp;
import com.bq.gametrade.http.VRHttpListener;
import com.bq.gametrade.ui.dialog.OpenRedEnvelopeDialog;
import com.bq.gametrade.utils.Base64Util;
import com.bq.gametrade.utils.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class RedEnvelopeListActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout layBack;
    private TextView tvTitle;
    private String  friendAccount = "";
    private ListView listView;
    private Button btnSendRedEnvelope;
    private ArrayList<RedEnvelopeListDetails> mList = new ArrayList<>();
    private LinearLayout layPrompt;
    private RedEnvelopeListAdapter adapter;
    private UserInfoDetails userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edrenvelope_list);
        if(application.isLogin()){
            userInfo = application.getUserInfo();
        }else {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }

        friendAccount = getIntent().getStringExtra("friendAccount");
        if ( friendAccount.equals("")) {
            toastShow("获取好友信息出错，请重试");
            finish();
        }
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getRedEnvelopeList();

    }

    private void initView() {
        layBack = serchViewById(R.id.lay_back);
        layBack.setOnClickListener(this);
        tvTitle = serchViewById(R.id.title_name);
        tvTitle.setText("红包列表");
        btnSendRedEnvelope = serchViewById(R.id.btn_sendRedEnvelope);
        btnSendRedEnvelope.setOnClickListener(this);
        layPrompt = serchViewById(R.id.lay_prompt);
        listView = serchViewById(R.id.listView_chat);
        adapter = new RedEnvelopeListAdapter(RedEnvelopeListActivity.this,mList,application);
        listView.setAdapter(adapter);

        adapter.setOnItemPressClickLitenerr(new RedEnvelopeListAdapter.OnItemPressClickListener() {
            @Override
            public void onItemPressClick(int position) {
                if(mList.get(position).revice_user.equals(application.getUserInfo().account)){
                    if(mList.get(position).m_status.equals("0")){
                        OpenRedEnvelopeDialog dialog = new OpenRedEnvelopeDialog();
                        Bundle bundle = new Bundle();
                        bundle.putParcelableArrayList("redEnvelopeList",mList);
                        bundle.putInt("position",position);
                        dialog.setArguments(bundle);
                        dialog.show(getSupportFragmentManager(),"dialog");
                    }else if(mList.get(position).m_status.equals("1")){
                        Intent intent = new Intent(RedEnvelopeListActivity.this, RedEnvelopeDetailsActivity.class);
                        intent.putParcelableArrayListExtra("redEnvelopeList",mList);
                        intent.putExtra("position",position);
                        startActivity(intent);
                    }

                }else {
                    Intent intent = new Intent(RedEnvelopeListActivity.this, RedEnvelopeDetailsActivity.class);
                    intent.putParcelableArrayListExtra("redEnvelopeList",mList);
                    intent.putExtra("position",position);
                    startActivity(intent);
                }

            }
        });

        adapter.setOnHeadImagePressClickListener(new RedEnvelopeListAdapter.OnHeadImagePressClickListener() {
            @Override
            public void OnHeadImageClick(int position) {
                if(mList.get(position).send_user.equals(application.getUserInfo().account)){
                    startActivity(new Intent(RedEnvelopeListActivity.this,PersonalDataActivity.class));
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lay_back:
                finish();
                break;
            case R.id.btn_sendRedEnvelope:
                Intent intent = new Intent(RedEnvelopeListActivity.this, SendRedEnvelopeActivity.class);
                intent.putExtra("friendAccount", friendAccount);
                startActivity(intent);
                break;

        }
    }


    public void getRedEnvelopeList() {
        Map<String, String> params = new HashMap<>();
        params.put("token", userInfo.token);
        params.put("account", friendAccount);
        params.put("curPage", "1");
        showSpotsDialog("正在加载·····");
        VRHttp.getRedEnvelopeList(RedEnvelopeListActivity.this, HttpLink.REDENVELOPE_LIST, params, new VRHttpListener() {
            @Override
            public void onSuccess(Object response, boolean isCache) {
                if (response != null) {
                    RedEnvelopeList redEnvelopeList = (RedEnvelopeList) response;
                    if (redEnvelopeList.status.equals("0")) {
                        mList = redEnvelopeList.luck_list;
                        if(mList.size()>0){
                            layPrompt.setVisibility(View.GONE);
                            adapter.setData(mList,application);
                            adapter.notifyDataSetChanged();
                            if(mList.size()<4){
                                listView.setSelectionFromTop(0,0);
                                listView.setSelection(0);
                            }else {
                                listView.setSelection(adapter.getCount());
                            }

                        }else {
                            layPrompt.setVisibility(View.VISIBLE);
                        }
                    }else if (redEnvelopeList.status.equals("99")) {
                        ToastUtils.showToast(RedEnvelopeListActivity.this ,"登录超时");
                        application.setUserInfo(null);
                        startActivity(new Intent(RedEnvelopeListActivity.this, LoginActivity.class));
                    } else if (redEnvelopeList.status.equals("98")) {
                        ToastUtils.showToast(RedEnvelopeListActivity.this, "您的账号已在其它设备登录，请重新登录修改您的密码");
                        application.setUserInfo(null);
                        startActivity(new Intent(RedEnvelopeListActivity.this, LoginActivity.class));
                    }
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();
                closeSpotsDialog();
            }

            @Override
            public void onError(String error) {
                super.onError(error);
                toastShow("获取失败请重试");
            }
        });
    }


}
